package com.cn.kehong.service;


import com.cn.kehong.api.ILineTaskService;
import com.cn.kehong.api.IProductChuciPeriodService;
import com.cn.kehong.api.IProductThickPeriodService;
import com.cn.kehong.api.IXunjianTaskService;
import com.cn.kehong.conf.Config;
import com.cn.kehong.dao.*;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.*;
import com.google.common.collect.Lists;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;


@EnableScheduling
@Service
public class XunjianTaskServiceImpl implements IXunjianTaskService {
    @Autowired
    private XunjianTaskMapper xunjianTaskMapper;
    @Autowired
    private Config config;


    @Autowired
    private ProductPeiTouMapper productPeiTouMapper;

    @Autowired
    private IProductThickPeriodService productThickPeriodService;

    @Autowired
    private IProductChuciPeriodService productChuciPeriodService;
    @Autowired
    private ProductPackageJobMapper productPackageJobMapper;
    @Autowired
    private LineTaskMapper lineTaskMapper;

    @Override
    public List<XunjianTask> getXunjianTaskList(QueryForm queryForm) throws Exception {
        queryForm.setNameStr(getNameStr(queryForm.getType()));
        int count = xunjianTaskMapper.getXunjianTaskCount(queryForm);
        if (count == 0) {
            return Lists.newArrayList();
        }
        List<XunjianTask> xunjianTasks = xunjianTaskMapper.getXunjianTaskList(queryForm);
        if (xunjianTasks.size() > 0) {
            xunjianTasks.get(0).setCount(count);
        }
        return xunjianTasks;
    }

    @Override
    public void exportXunjianTask(QueryForm queryForm, HttpServletResponse response) throws Exception {
        Map<String, Object> result = Maps.newHashMap();
        queryForm.setNameStr(getNameStr(queryForm.getType()));
        List<XunjianTask> xunjianTasks = xunjianTaskMapper.getXunjianTaskList(queryForm);
        result.put("xunjianTasks", xunjianTasks);
        result.put("title", queryForm.getNameStr());
        String fileName = CommonUtil.generateRandomNum("巡检-") + ".xlsx";
        String filePath = config.filePath + fileName;
        String nameStr = queryForm.getNameStr() + ".xlsx";
        //混料工序
        if (queryForm.getType().equals("0")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "types.xlsx", response);
        }
//        else if (queryForm.getType().equals("1")) {
//            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "types1.xlsx", response);
//        }
        //浆料过筛有无杂质
        else if (queryForm.getType().equals("1")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "types2.xlsx", response);
        }//干燥工序
        else if (queryForm.getType().equals("4")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "types3.xlsx", response);
        }//物料外观
        else if (queryForm.getType().equals("5")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "types4.xlsx", response);
        }//炉内温区氮气流量
        else if (queryForm.getType().equals("8")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "types5.xlsx", response);
        }// 炉内温区含氧量
        else if (queryForm.getType().equals("9")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "types6.xlsx", response);
        }//窑炉出料温度
        else if (queryForm.getType().equals("10")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "types7.xlsx", response);
        }//包装工序
        else if (queryForm.getType().equals("15")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "types8.xlsx", response);
        }//投料巡检
        else if (queryForm.getType().equals("2")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "tlxjTypes.xlsx", response);
        }//浆料过筛
        else if (queryForm.getType().equals("3")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "jlgsTypes.xlsx", response);
        }//干燥工序巡检1
        else if (queryForm.getType().equals("6")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "gzgxxj1Types.xlsx", response);
        }//烧结工序巡检1
        else if (queryForm.getType().equals("11")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "sjgxxj1Types.xlsx", response);
        }//烧结工序巡检2
        else if (queryForm.getType().equals("12")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "sjgxxj2Types.xlsx", response);
        }//烧结工序物料颜色巡检
        else if (queryForm.getType().equals("13")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "sjgxysTypes.xlsx", response);
        }//分级电除磁
        else if (queryForm.getType().equals("14")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "fjdccTypes.xlsx", response);
        }//包装工序巡检
        else if (queryForm.getType().equals("16")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "bzgxxjTypes.xlsx", response);
        }
    }

//  @Override
//  public List<XunjianTask> getXunjianTaskListByApp(QueryForm queryForm) throws Exception {
//    int count = xunjianTaskMapper.getXunjianTaskCountByApp(queryForm);
//    if(count == 0){
//      return Lists.newArrayList();
//    }
//    List<XunjianTask> list = xunjianTaskMapper.getXunjianTaskListByApp(queryForm);
//    list.get(0).setCount(count);
//    return list;
//  }

    @Override
    public Map<String, Object> getXunjianTaskCountByApp(QueryForm queryForm) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String factoryName = queryForm.getFactoryName();
        XunjianTaskExample example = new XunjianTaskExample();
        example.createCriteria().andLineTypeEqualTo("1").andXjStatusEqualTo("0").andFactoryNameEqualTo(factoryName);
        //批次
        int pc = xunjianTaskMapper.countByExample(example);
        //本班
        XunjianTaskExample example1 = new XunjianTaskExample();
        example1.createCriteria().andLineTypeEqualTo("3").andXjStatusEqualTo("0").andFactoryNameEqualTo(factoryName);
        int bb = xunjianTaskMapper.countByExample(example1);
        //今日或者其他
        int qt = xunjianTaskMapper.countByTodayOrqt();
        //单包数量
        int db = productPackageJobMapper.getCountByApp(factoryName);
        int pk = lineTaskMapper.getProductionCompletedByPkCount(queryForm.getFactoryName());
//        ProductPackageJobExample productPackageJobExample = new ProductPackageJobExample();
//        productPackageJobExample.createCriteria().andJobStatusEqualTo("0");
//        int db = productPackageJobMapper.countByExample(productPackageJobExample);
        resultMap.put("db", db);
        resultMap.put("pk", pk);
        resultMap.put("pc", pc);
        resultMap.put("bb", bb);
        resultMap.put("qt", qt);
        return resultMap;
    }

    @Override
    public List<XunjianTask> getNameByXunjianList(QueryForm queryForm) throws Exception {
        XunjianTaskExample taskExample = new XunjianTaskExample();
        taskExample.createCriteria().andFactoryNameEqualTo(queryForm.getFactoryName()).andTaskNameEqualTo(queryForm.getTaskName());
        List<XunjianTask> xunjianTasks = xunjianTaskMapper.selectByExample(taskExample);
        return xunjianTasks;
    }

    //    @Override
//    public Map<String, Object> getXunjianTaskListByApp(QueryForm queryForm) throws Exception {
//        Map<String, Object> resultMap = new HashMap<>();
//        String factoryName = queryForm.getFactoryName();
//        // 品控巡检任务---批次任务
//        if (queryForm.getType().equals("1")) {
//            // 批次任务待完成
//            List<XunjianTask> completedList = xunjianTaskMapper.getXunjianTaskListByPc(factoryName);
//            if (completedList.size() > 0) {
//                completedList.get(0).setCount(completedList.size());
//                resultMap.put("CompletedList", completedList);
//            } else {
//                resultMap.put("CompletedList", new ArrayList<>());
//            }
//            // 批次任务已完成
//            List<XunjianTask> isCompletedList = xunjianTaskMapper.getXunjianTaskListByPc1(factoryName);
//            if (isCompletedList.size() > 0) {
//                isCompletedList.get(0).setCount(isCompletedList.size());
//                resultMap.put("isCompletedList", isCompletedList);
//            } else {
//                resultMap.put("isCompletedList", new ArrayList<>());
//            }
//            // 品控巡检任务---本班任务
//        } else if (queryForm.getType().equals("2")) {
//            // 品控巡检任务待完成
//            List<XunjianTask> completedList = xunjianTaskMapper.getXunjianTaskListByBb(factoryName);
//            if (completedList.size() > 0) {
//                completedList.get(0).setCount(completedList.size());
//                resultMap.put("CompletedList", completedList);
//            } else {
//                resultMap.put("CompletedList", new ArrayList<>());
//            }
//            // 品控巡检任务已完成
//            List<XunjianTask> isCompletedList = xunjianTaskMapper.getXunjianTaskListByBb1(factoryName);
//            if (isCompletedList.size() > 0) {
//                isCompletedList.get(0).setCount(isCompletedList.size());
//                resultMap.put("isCompletedList", isCompletedList);
//            } else {
//                resultMap.put("isCompletedList", new ArrayList<>());
//            }
//            //品控巡检任务---其他或今日
//        } else if (queryForm.getType().equals("3")) {
//            // 其他或今日待完成
//            List<XunjianTask> completedList = xunjianTaskMapper.getXunjianTaskListByJrOrQt(factoryName);
//            if (completedList.size() > 0) {
//                completedList.get(0).setCount(completedList.size());
//                resultMap.put("CompletedList", completedList);
//            } else {
//                resultMap.put("CompletedList", new ArrayList<>());
//            }
//            // 其他或今日已完成
//            List<XunjianTask> isCompletedList = xunjianTaskMapper.getXunjianTaskListByJrOrQt1(factoryName);
//            if (isCompletedList.size() > 0) {
//                isCompletedList.get(0).setCount(isCompletedList.size());
//                resultMap.put("isCompletedList", isCompletedList);
//            } else {
//                resultMap.put("isCompletedList", new ArrayList<>());
//            }
//        }
//        return resultMap;
//    }
    @Override
    public Map<String, Object> getXunjianTaskListByApp(QueryForm queryForm) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String factoryName = queryForm.getFactoryName();
        // 品控巡检任务---批次任务
        if (queryForm.getType().equals("1")) {
            // 批次任务待完成
            List<XunjianTask> completedList = xunjianTaskMapper.getXunjianTaskListByPc(queryForm);
            if (completedList.size() > 0) {
                completedList.get(0).setCount(completedList.size());
                resultMap.put("CompletedList", completedList);
            } else {
                resultMap.put("CompletedList", new ArrayList<>());
            }
            // 批次任务已完成
            List<XunjianTask> isCompletedList = xunjianTaskMapper.getXunjianTaskListByPc1(queryForm);
            if (isCompletedList.size() > 0) {
                isCompletedList.get(0).setCount(isCompletedList.size());
                resultMap.put("isCompletedList", isCompletedList);
            } else {
                resultMap.put("isCompletedList", new ArrayList<>());
            }
            // 品控巡检任务---本班任务
        } else if (queryForm.getType().equals("2")) {
            // 品控巡检任务待完成
            List<XunjianTask> completedList = xunjianTaskMapper.getXunjianTaskListByBb(queryForm);
            if (completedList.size() > 0) {
                completedList.get(0).setCount(completedList.size());
                resultMap.put("CompletedList", completedList);
            } else {
                resultMap.put("CompletedList", new ArrayList<>());
            }
            // 品控巡检任务已完成
            List<XunjianTask> isCompletedList = xunjianTaskMapper.getXunjianTaskListByBb1(queryForm);
            if (isCompletedList.size() > 0) {
                isCompletedList.get(0).setCount(isCompletedList.size());
                resultMap.put("isCompletedList", isCompletedList);
            } else {
                resultMap.put("isCompletedList", new ArrayList<>());
            }
            //品控巡检任务---其他或今日
        } else if (queryForm.getType().equals("3")) {
            // 其他或今日待完成
            List<XunjianTask> completedList = xunjianTaskMapper.getXunjianTaskListByJrOrQt(queryForm);
            if (completedList.size() > 0) {
                completedList.get(0).setCount(completedList.size());
                resultMap.put("CompletedList", completedList);
            } else {
                resultMap.put("CompletedList", new ArrayList<>());
            }
            // 其他或今日已完成
            List<XunjianTask> isCompletedList = xunjianTaskMapper.getXunjianTaskListByJrOrQt1(queryForm);
            if (isCompletedList.size() > 0) {
                isCompletedList.get(0).setCount(isCompletedList.size());
                resultMap.put("isCompletedList", isCompletedList);
            } else {
                resultMap.put("isCompletedList", new ArrayList<>());
            }
        }
        return resultMap;
    }

    @Override
    public String addXunjianTask(XunjianTask xunjianTask) throws Exception {
        if (null == xunjianTask) {
            return "请输入巡检任务";
        }
        String result = this.xunjianTaskAddChange(xunjianTask);
        if (!"".equals(result)) {
            return result;
        }
        String id = CommonUtil.generateRandomNum("xunjianTask_");
        xunjianTask.setId(id);
        xunjianTask.setStatus("0");
        xunjianTask.setLineType("5");
        xunjianTask.setCreateTime(new Date());
        int count = xunjianTaskMapper.insertSelective(xunjianTask);

        if (count < 1) {
            return "新增失败！";
        }
        return CommonUtil.outStr(count);
    }

    @Override
    public String addXunjianYaoLuTask(String productCode,String factoryName) throws Exception {
        XunjianTaskExample example = new XunjianTaskExample();
        example.createCriteria().andProductCodeEqualTo(productCode).andStatusEqualTo(Constants.NORMAL_STATUS).andTaskNameEqualTo("烧结工序物料颜色巡检");
        int count = xunjianTaskMapper.countByExample(example);
        if(count > 0){
            return "";
        }
        Map<String,String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String whiteDay = dutyMap.get("whiteDay");
        XunjianTask xunjianTask = new XunjianTask();
        xunjianTask.setId(CommonUtil.generateRandomNum("xunjianTask_-"));
        xunjianTask.setFactoryName(factoryName);
        xunjianTask.setLineType("1");
        xunjianTask.setWorkPeriod("烧结");
        xunjianTask.setWorkProcess("");
        xunjianTask.setBanCi(StringUtils.equals(whiteDay,"1")?"A":"B");
        xunjianTask.setTaskName("烧结工序物料颜色巡检");
        xunjianTask.setFrequency("1次/批");
        xunjianTask.setProductCode(productCode);
        count = xunjianTaskMapper.insertSelective(xunjianTask);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateLineTask(XunjianTask xunjianTask) throws Exception {
        String id = xunjianTask.getId();
        if (StringUtils.isEmpty(id)) {
            return "id is null";
        }
        String result = this.xunjianTaskUpdateChange(xunjianTask);
        if (!"".equals(result)) {
            return result;
        }
        XunjianTaskExample example = new XunjianTaskExample();
        example.createCriteria().andIdEqualTo(id);
        xunjianTask.setUpdateTime(new Date());
        int count = xunjianTaskMapper.updateByExampleSelective(xunjianTask, example);
        return CommonUtil.outStr(count);
    }

    @Override
    public Map<String, Integer> getXunjianTaskToday() throws Exception {
        LocalDateTime startDateTime = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endDateTime = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startTime = startDateTime.format(formatter);
        String endTime = endDateTime.format(formatter);
        Map<String, Integer> taskCounts = new HashMap<>();
        QueryForm queryForm = new QueryForm();
        queryForm.setXjStatus("0");
        queryForm.setStartTime(startTime);
        queryForm.setEndTime(endTime);
        String[] taskNames = {"炉内温区含氧量", "炉内温区氮气流量", "窑炉出料温度", "干燥工序巡检1", "浆料过筛有无杂质", "混料工序", "物料外观", "包装工序"};
        for (String taskName : taskNames) {
            queryForm.setTaskName(taskName);
            int count = xunjianTaskMapper.getcountByToday(queryForm);
            taskCounts.put(taskName, count);
        }

        return taskCounts;
    }

    @Override
    public String xunjianTaskCancel(String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            return "主键不能为空";
        }
        XunjianTask xunjianTask = new XunjianTask();
        xunjianTask.setUpdateTime(new Date());
        xunjianTask.setXjStatus("4");
        XunjianTaskExample example = new XunjianTaskExample();
        example.createCriteria().andIdEqualTo(id);
        int count = xunjianTaskMapper.updateByExampleSelective(xunjianTask, example);
        if (count < 1) {
            return "取消失败！";
        }
        return CommonUtil.outStr(count);
    }

    @Override
    public Map<String, Object> getXunjianTaskByNewCount(QueryForm queryForm) throws Exception {
        //线边任务
        Map<String, Object> resultMap = new HashMap<>();
        ArrayList<String> MaList = new ArrayList<>();
        MaList.add("混料工序");
        MaList.add("浆料过筛有无杂质");
        MaList.add("投料巡检");
        MaList.add("浆料过筛");

        ArrayList<String> MbList = new ArrayList<>();
        MbList.add("干燥工序");
        MbList.add("物料外观");
        MbList.add("干燥工序巡检1");
        MbList.add("干燥工序巡检2");

        ArrayList<String> McList = new ArrayList<>();
        McList.add("炉内温区氮气流量");
        McList.add("炉内温区含氧量");
        McList.add("窑炉出料温度");
        McList.add("烧结工序巡检1");
        McList.add("烧结工序巡检2");
        McList.add("烧结工序物料颜色巡检");
        McList.add("分级电除磁");

        ArrayList<String> MdList = new ArrayList<>();
        MdList.add("包装工序");
        MdList.add("包装工序巡检");

        XunjianTaskExample example = new XunjianTaskExample();
        example.createCriteria().andTaskNameIn(MaList).andXjStatusEqualTo("0");
        int MaCount = xunjianTaskMapper.countByExample(example);
        resultMap.put("MaCount", MaCount);

        XunjianTaskExample example1 = new XunjianTaskExample();
        example1.createCriteria().andTaskNameIn(MbList).andXjStatusEqualTo("0");
        int MbCount = xunjianTaskMapper.countByExample(example1);
        resultMap.put("MbCount", MbCount);

        XunjianTaskExample example2 = new XunjianTaskExample();
        example2.createCriteria().andTaskNameIn(McList).andXjStatusEqualTo("0");
        int McCount = xunjianTaskMapper.countByExample(example2);
        resultMap.put("McCount", McCount);

        XunjianTaskExample example3 = new XunjianTaskExample();
        example3.createCriteria().andTaskNameIn(MdList).andXjStatusEqualTo("0");
        int MdCount = xunjianTaskMapper.countByExample(example3);
        resultMap.put("MdCount", MdCount);
        return resultMap;
    }

    @Override
    public Map<String, Object> getXunjianTaskByNewType(QueryForm queryForm) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        if (queryForm.getType().equals("MA")) {
            String[] types = {"0", "1", "2", "3"};
            String[] names = {"hlgx", "jlgsywzz", "tlxj", "jlgs"};
            for (int i = 0; i < types.length; i++) {
                queryForm.setType(types[i]);
                queryForm.setNameStr(getNameStr(queryForm.getType()));
                //待完成的数据
                queryForm.setXjStatus("0");
                int count = xunjianTaskMapper.getXunjianTaskCount(queryForm);
                resultMap.put(names[i] + "Count", count);
            }
        } else if (queryForm.getType().equals("MB")) {
            String[] types = {"4", "5", "6", "7"};
            String[] names = {"gzgx", "wlwg", "gzgxxj1", "gzgxxj2"};
            for (int i = 0; i < types.length; i++) {
                queryForm.setType(types[i]);
                queryForm.setNameStr(getNameStr(queryForm.getType()));
                //待完成的数据
                queryForm.setXjStatus("0");
                int count = xunjianTaskMapper.getXunjianTaskCount(queryForm);
                resultMap.put(names[i] + "Count", count);
            }
        } else if (queryForm.getType().equals("MC")) {
            String[] types = {"8", "9", "10", "11", "12", "13", "14"};
            String[] names = {"lnwqtqll", "lnwqhyl", "ylclwd", "sjgxxj1", "sjgxxj2", "sjgxwlysxj", "fjdcc"};
            for (int i = 0; i < types.length; i++) {
                queryForm.setType(types[i]);
                queryForm.setNameStr(getNameStr(queryForm.getType()));
                //待完成的数据
                queryForm.setXjStatus("0");
                int count = xunjianTaskMapper.getXunjianTaskCount(queryForm);
                resultMap.put(names[i] + "Count", count);
            }
        } else if (queryForm.getType().equals("MD")) {
            String[] types = {"15", "16"};
            String[] names = {"bzgx", "bzgxxj"};
            for (int i = 0; i < types.length; i++) {
                queryForm.setType(types[i]);
                queryForm.setNameStr(getNameStr(queryForm.getType()));
                //待完成的数据
                queryForm.setXjStatus("0");
                int count = xunjianTaskMapper.getXunjianTaskCount(queryForm);
                resultMap.put(names[i] + "Count", count);
            }
        }
        return resultMap;
    }


    @Scheduled(cron = "*/50 * * * * ?")
    public void updateScheduledLineTask() throws Exception {
//        List<ProductThickPeriod> productThickPeriods = productThickPeriodService.getXunTask();
        List<XunjianTask> xunjianTasks = Lists.newArrayList();
//        if (CollectionsUtil.isNotEmpty(productThickPeriods)) {
//            List<String> thickIds = Lists.newArrayList();
//            for (ProductThickPeriod thickPeriod : productThickPeriods) {
//                XunjianTask xunjianTask = new XunjianTask();
//                xunjianTask.setAttr16(thickPeriod.getId());
//                xunjianTask.setFactoryName(thickPeriod.getFactoryName());
//                xunjianTask.setLineType("1");
//                xunjianTask.setWorkPeriod("混料");
//                xunjianTask.setWorkProcess("细磨");
//                xunjianTask.setProductCode(thickPeriod.getStartCode());
//                String banCi = StringUtils.equals(thickPeriod.getBanCi(), "1") ? "A" : "B";
//                xunjianTask.setBanCi(banCi);
//                xunjianTask.setTaskName("砂磨机料仓压力");
//                xunjianTask.setFrequency("1次/批");
//                xunjianTask.setId(CommonUtil.generateRandomNum("xjtask-"));
//                xunjianTasks.add(xunjianTask);
//                thickIds.add(thickPeriod.getId());
//            }
//            String resp = productThickPeriodService.updateThickPeriodByIds("2",thickIds);
//            if (StringUtils.isNotEmpty(resp)) {
//                return;
//            }
//        }
        List<XunjianTask> xunjianTasks1 = xunjianTaskMapper.selectXunjTasksByType();
        Map<String, XunjianTask> latestXunJLineTaskMap = Maps.newHashMap();
        for (XunjianTask task : xunjianTasks1) {
            String factoryName = task.getFactoryName();
            String taskName = task.getTaskName();
            if (latestXunJLineTaskMap.containsKey(factoryName + "-" + taskName)) {
                continue;
            }

            latestXunJLineTaskMap.put(factoryName + "-" + taskName, task);
        }

        Calendar calendar = Calendar.getInstance();
        int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);

        if ((hourOfDay >= 9 && hourOfDay < 19) || (hourOfDay >= 21)) {
            this.addLineBanciTask("16-1", "干燥工序巡检1", "", "干燥", xunjianTasks, latestXunJLineTaskMap);
            this.addLineBanciTask("16-2", "干燥工序巡检1", "", "干燥", xunjianTasks, latestXunJLineTaskMap);


            this.addLineBanciTask("16-1", "炉内温区氮气流量", "烧结", "", xunjianTasks, latestXunJLineTaskMap);
            this.addLineBanciTask("16-2", "炉内温区氮气流量", "烧结", "", xunjianTasks, latestXunJLineTaskMap);

            this.addLineBanciTask("16-1", "炉内温区含氧量", "烧结", "", xunjianTasks, latestXunJLineTaskMap);
            this.addLineBanciTask("16-2", "炉内温区含氧量", "烧结", "", xunjianTasks, latestXunJLineTaskMap);
        }

        this.addLineDayTask("16-1", "浆料过筛有无杂质", "混料", "细磨", xunjianTasks, latestXunJLineTaskMap);
        this.addLineDayTask("16-2", "浆料过筛有无杂质", "混料", "细磨", xunjianTasks, latestXunJLineTaskMap);

        this.addLineDayTask("16-1", "窑炉出料温度", "", "烧结", xunjianTasks, latestXunJLineTaskMap);
        this.addLineDayTask("16-2", "窑炉出料温度", "", "烧结", xunjianTasks, latestXunJLineTaskMap);

        this.addLineDayTask("16-1", "浆料过筛", "混料", "", xunjianTasks, latestXunJLineTaskMap);
        this.addLineDayTask("16-2", "浆料过筛", "混料", "", xunjianTasks, latestXunJLineTaskMap);
        if (CollectionsUtil.isNotEmpty(xunjianTasks)) {
            int count = xunjianTaskMapper.batchInsertXunjianTasks(xunjianTasks);
            if (count < 0) {
                throw new Exception("插入巡检表失败");
            }
        }
    }

    private void addLineBanciTask(String factoryName, String taskName, String workPeriod, String process, List<XunjianTask> lineTasks, Map<String, XunjianTask> latestLineTaskMap) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today = simpleDateFormat.format(new Date());
        Map<String, String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String banci = StringUtils.equals(dutyMap.get("whiteDay"), "1") ? "A" : "B";
        boolean szql = false;
        if (latestLineTaskMap.containsKey(factoryName + "-" + taskName)) {
            XunjianTask task = latestLineTaskMap.get(factoryName + "-" + taskName);
            String createTime = simpleDateFormat.format(task.getCreateTime());
            if (!StringUtils.equals(today, createTime) || !StringUtils.equals(task.getBanCi(), banci)) {
                szql = true;
            }
        } else {
            szql = true;
        }
        if (szql) {
            if (StringUtils.equals("炉内温区氮气流量", taskName) || StringUtils.equals("炉内温区含氧量", taskName)) {
                for (int i = 1; i <= 12; i++) {
                    XunjianTask xunjianTask = new XunjianTask();
                    xunjianTask.setFactoryName(factoryName);
                    xunjianTask.setLineType("3");
                    xunjianTask.setWorkPeriod(workPeriod);
                    xunjianTask.setWorkProcess(process);
                    xunjianTask.setBanCi(banci);
                    xunjianTask.setTaskName(taskName);
                    xunjianTask.setFrequency("1次/班");
                    xunjianTask.setAttr1(i + "");
                    xunjianTask.setId(CommonUtil.generateRandomNum("xunjianTask_-"));
                    lineTasks.add(xunjianTask);
                }
            } else {
                for (int i = 1; i <= 6; i++) {
                    XunjianTask xunjianTask = new XunjianTask();
                    xunjianTask.setFactoryName(factoryName);
                    xunjianTask.setLineType("3");
                    xunjianTask.setWorkPeriod(workPeriod);
                    xunjianTask.setWorkProcess(process);
                    xunjianTask.setBanCi(banci);
                    xunjianTask.setTaskName(taskName);
                    xunjianTask.setFrequency("1次/班");
                    xunjianTask.setAttr1(i + "");
                    xunjianTask.setId(CommonUtil.generateRandomNum("xunjianTask_-"));
                    lineTasks.add(xunjianTask);
                }
            }
        }
    }

    private void addLineDayTask(String factoryName, String taskName, String workPeriod, String process, List<XunjianTask> lineTasks, Map<String, XunjianTask> latestLineTaskMap) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today = simpleDateFormat.format(new Date());
        boolean szql = false;
        if (latestLineTaskMap.containsKey(factoryName + "-" + taskName)) {
            XunjianTask task = latestLineTaskMap.get(factoryName + "-" + taskName);
            String createTime = simpleDateFormat.format(task.getCreateTime());
            if (!StringUtils.equals(createTime, today)) {
                szql = true;
            }
        }else {
            szql = true;
        }
        Map<String, String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String banci = StringUtils.equals(dutyMap.get("whiteDay"), "1") ? "A" : "B";
        if (szql) {
            if (StringUtils.equals("浆料过筛", taskName)) {
                for (int i = 5; i <= 15; i++) {
                    XunjianTask xunjianTask = new XunjianTask();
                    xunjianTask.setFactoryName(factoryName);
                    xunjianTask.setLineType("2");
                    xunjianTask.setWorkPeriod(workPeriod);
                    xunjianTask.setWorkProcess(process);
                    xunjianTask.setBanCi(banci);
                    xunjianTask.setTaskName(taskName);
                    xunjianTask.setFrequency("1次/日");
                    xunjianTask.setAttr1(i + "");
                    xunjianTask.setId(CommonUtil.generateRandomNum("xunjianTask_-"));
                    lineTasks.add(xunjianTask);
                }
            } else {
                XunjianTask addLineTask = new XunjianTask();
                addLineTask.setFactoryName(factoryName);
                addLineTask.setLineType("2");
                addLineTask.setWorkProcess(workPeriod);
                addLineTask.setWorkProcess(process);
                addLineTask.setBanCi(banci);
                addLineTask.setTaskName(taskName);
                addLineTask.setFrequency("1次/日");
                addLineTask.setId(CommonUtil.generateRandomNum("xunjianTask_-"));
                lineTasks.add(addLineTask);

            }
        }
    }


    public String getNameStr(String type) {
        if (type.equals("0")) {
            return "混料工序";
        } else if (type.equals("1")) {
            return "浆料过筛有无杂质";
        } else if (type.equals("2")) {
            return "投料巡检";
        } else if (type.equals("3")) {
            return "浆料过筛";
        } else if (type.equals("4")) {
            return "干燥工序";
        } else if (type.equals("5")) {
            return "物料外观";
        } else if (type.equals("6")) {
            return "干燥工序巡检1";
        } else if (type.equals("7")) {
            return "干燥工序巡检2";
        } else if (type.equals("8")) {
            return "炉内温区氮气流量";
        } else if (type.equals("9")) {
            return "炉内温区含氧量";
        } else if (type.equals("10")) {
            return "窑炉出料温度";
        } else if (type.equals("11")) {
            return "烧结工序巡检1";
        } else if (type.equals("12")) {
            return "烧结工序巡检2";
        } else if (type.equals("13")) {
            return "烧结工序物料颜色巡检";
        } else if (type.equals("14")) {
            return "分级电除磁";
        } else if (type.equals("15")) {
            return "包装工序";
        } else if (type.equals("16")) {
            return "包装工序巡检";
        }
        return "";
    }


    private String xunjianTaskAddChange(XunjianTask xunjianTask) {

        String result = "";

        if (StringUtils.isBlank(xunjianTask.getFactoryName())) {
            result = "请输入厂房车间";
            return result;
        }
        return result;


    }

    //修改必填项
    private String xunjianTaskUpdateChange(XunjianTask xunjianTask) {

        String result = "";

        if (xunjianTask.getTaskName().equals("混料工序")) {
            if (StringUtils.isBlank(xunjianTask.getAttr1())) {
                result = "请输入搅拌杆转速";
                return result;
            }
            if (StringUtils.isBlank(xunjianTask.getAttr2())) {
                result = "请输入浆料颜色";
                return result;
            }
        } else if (xunjianTask.getTaskName().equals("砂磨机料仓压力")) {
            if (StringUtils.isBlank(xunjianTask.getAttr1())) {
                result = "请选择砂磨机号";
                return result;
            }
            if (StringUtils.isBlank(xunjianTask.getAttr2())) {
                result = "请输入料仓压力";
                return result;
            }
            if (StringUtils.isBlank(xunjianTask.getAttr3())) {
                result = "请输入仓压判定";
                return result;
            }
        } else if (xunjianTask.getTaskName().equals("物料外观")) {
            if (StringUtils.isBlank(xunjianTask.getAttr1())) {
                result = "请输入物料外观";
                return result;
            }
        } else if (xunjianTask.getTaskName().equals("炉内温区氮气流量")) {
            if (StringUtils.isBlank(xunjianTask.getAttr1())) {
                result = "请输入窑炉号";
                return result;
            }
            if (StringUtils.isBlank(xunjianTask.getAttr2())) {
                result = "请输入前门";
                return result;
            }
        } else if (xunjianTask.getTaskName().equals("炉内温区含氧量")) {
            if (StringUtils.isBlank(xunjianTask.getAttr1())) {
                result = "请输入窑炉号";
                return result;
            }
            if (StringUtils.isBlank(xunjianTask.getAttr2())) {
                result = "请输入氮气进气流量";
                return result;
            }
            if (StringUtils.isBlank(xunjianTask.getAttr3())) {
                result = "请输入温区1";
                return result;
            }
            if (StringUtils.isBlank(xunjianTask.getAttr4())) {
                result = "请输入温区2";
                return result;
            }
            if (StringUtils.isBlank(xunjianTask.getAttr5())) {
                result = "请输入温区3";
                return result;
            }
        } else if (xunjianTask.getTaskName().equals("窑炉出料温度")) {
            if (StringUtils.isBlank(xunjianTask.getAttr1())) {
                result = "请输入窑炉号";
                return result;
            }
            if (StringUtils.isBlank(xunjianTask.getAttr2())) {
                result = "请输入压缩空气压力";
                return result;
            }
            if (StringUtils.isBlank(xunjianTask.getAttr3())) {
                result = "请输入循环时间";
                return result;
            }
            if (StringUtils.isBlank(xunjianTask.getAttr4())) {
                result = "请输入上层出料温度";
                return result;
            }
            if (StringUtils.isBlank(xunjianTask.getAttr5())) {
                result = "请输入下层出料温度";
                return result;
            }
        }
        return result;


    }
}
